/*
 * Copyright (c) 2010, Davide Brugali, All right reserved
 * 
 * Project MotionPlanning
 * 
 * Package motionplanning.core
 * 
 * Walks the Tree in pre-order style. This is a recursive method, and is
 * called from the toList() method with the root element as the first
 * argument. It appends to the second argument, which is passed by reference     * as it recurses down the tree.
 */
package star.core.util.graph;

import java.util.Iterator;

public class GTIterator<T> implements Iterator<T> {
	private GTNode<T> current = null;
	private T data = null;

	/**
	 * @param tree
	 */
	public GTIterator(GeneralTree<T> tree) {
		super();
		this.current = tree.getRootElement();
	}
	
	public GTIterator(GTNode<T> rootNode) {
		super();
		this.current = rootNode;
	}	
	
	public boolean hasNext() {
		if(this.current != null)
			return true;
		return false;
	}
	
	public T next() {
		if(current == null)
			return null;		
		data = current.value();
		if(current.child() != null)
			current = current.child();
		else if(current.sibling() != null)
			current = current.sibling();
		else
			recursiveNext(current);
		return data;
	}
	
	private void recursiveNext(GTNode<T> node) {
		if(node.parent() == null)
			current = null;
		else if(node.parent().sibling() != null)
			current = node.parent().sibling();
		else 
			recursiveNext(node.parent());
	}

	
	public void remove() {
		// TODO Auto-generated method stub
		
	}
}
